package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IListContractService;
import com.mdd.admin.validate.ListContractCreateValidate;
import com.mdd.admin.validate.ListContractSearchValidate;
import com.mdd.admin.validate.ListContractUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ListContractDetailVo;
import com.mdd.admin.vo.ListContractListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ListContract;
import com.mdd.common.mapper.ListContractMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 新建合同扩展信息实现类
 * @author LikeAdmin
 */
@Service
public class ListContractServiceImpl implements IListContractService {
        
    @Resource
    ListContractMapper listContractMapper;

    /**
     * 新建合同扩展信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ListContractListedVo>
     */
    @Override
    public PageResult<ListContractListedVo> list(PageValidate pageValidate, ListContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ListContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        listContractMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:ownershipDistribution@ownership_distribution:str",
            "=:ownershipDistributionId@ownership_distribution_id:long",
            "=:importanceDegree@importance_degree:str",
            "=:partyaEmail@partyA_email:str",
            "=:totalContractFees@total_contract_fees:str",
            "=:putdownOfficialfee@putDown_officialFee:str",
            "=:putdownAgencyfee@putDown_agencyFee:str",
            "=:putdownRental@putDown_rental:str",
            "=:termOfPayment@term_of_payment:str",
            "=:termOfPaymentid@term_of_paymentId:long",
            "=:advance:str",
            "=:termOfpaymentExplain@term_ofPayment_explain:str",
            "=:billingMethod@billing_method:str",
            "=:invoiceTitle@invoice_title:str",
            "=:numberOfInventions@number_of_inventions:long",
            "=:inventionCommission@Invention_commission:str",
            "=:miscDigitalComponents@misc_Digital_Components:long",
            "=:otherAgencyFees@other_agency_fees:str",
            "=:otherHelp@other_help:str",
            "=:contractParticulars@contract_particulars:str",
            "=:partybEmail@PartyB_email:str",
            "=:contractAgencyFee@contract_agency_fee:str",
            "=:intermediateOfficialFees@Intermediate_official_fees:str",
            "=:intermediaryAgencyFee@Intermediary_agency_fee:str",
            "=:totalIntermediateFunds@total_intermediate_funds:str",
            "=:applicationPaymentMethod@application_payment_method:str",
            "=:applicationPaymentMethodid@application_payment_methodId:long",
            "=:isExaminationFee@is_examination_fee:long",
            "=:agencySettlementMethod@agency_settlement_method:str",
            "=:agencySettlementMethodid@agency_settlement_methodId:long",
            "=:numberDoubleReports@number_double_reports:long",
            "=:doubleTotalAgency@double_total_agency:str",
            "=:numberNewPieces@number_new_pieces:long",
            "=:newAgencyFee@new_agency_fee:str",
            "=:declarationArea@declaration_area:str",
            "eq:declarationTerm@declaration_term:str",
            "=:declarationRequirement@declaration_requirement:str",
            "=:accountCredited@account_credited:str",
            "=:accountCreditedId@account_credited_id:long",
            "=:serviceStandard@service_standard:str",
            "=:finalOfficialFee@final_official_fee:str",
            "=:finalAgencyFee@final_agency_fee:str",
            "=:balance:str",
            "=:licensingPaymentMethod@licensing_payment_method:str",
            "=:firstthreePaymentMethod@firstThree_payment_method:str",
            "=:doubleInventionAgency@double_invention_agency:str",
            "=:doubleReportAgency@double_report_agency:str",
            "=:numberAppearancePieces@number_appearance_pieces:long",
            "=:appearanceCommission@appearance_commission:str",
            "=:projectConsultant@project_consultant:str",
            "=:annualRegulatoryRequirements@annual_regulatory_requirements:str",
            "=:userId@user_id:long",
            "=:updateUserId@update_user_id:long",
            "like:updateUserName@update_user_name:str",
            "like:userName@user_name:str",
        });

        IPage<ListContract> iPage = listContractMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ListContractListedVo> list = new LinkedList<>();
        for(ListContract item : iPage.getRecords()) {
            ListContractListedVo vo = new ListContractListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 新建合同扩展信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ListContract
     */
    @Override
    public ListContractDetailVo detail(Integer id) {
        ListContract model = listContractMapper.selectOne(
                new QueryWrapper<ListContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ListContractDetailVo vo = new ListContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 新建合同扩展信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ListContractCreateValidate createValidate) {
        ListContract model = new ListContract();

        model.setOwnershipDistribution(createValidate.getOwnershipDistribution());
        model.setOwnershipDistributionId(createValidate.getOwnershipDistributionId());
        model.setImportanceDegree(createValidate.getImportanceDegree());
        model.setPartyaEmail(createValidate.getPartyaEmail());
        model.setTotalContractFees(createValidate.getTotalContractFees());
        model.setPutdownOfficialfee(createValidate.getPutdownOfficialfee());
        model.setPutdownAgencyfee(createValidate.getPutdownAgencyfee());
        model.setPutdownRental(createValidate.getPutdownRental());
        model.setTermOfPayment(createValidate.getTermOfPayment());
        model.setAdvance(createValidate.getAdvance());
        model.setTermOfpaymentExplain(createValidate.getTermOfpaymentExplain());
        model.setBillingMethod(createValidate.getBillingMethod());
        model.setInvoiceTitle(createValidate.getInvoiceTitle());
        model.setNumberOfInventions(createValidate.getNumberOfInventions());
        model.setInventionCommission(createValidate.getInventionCommission());
        model.setMiscDigitalComponents(createValidate.getMiscDigitalComponents());
        model.setOtherAgencyFees(createValidate.getOtherAgencyFees());
        model.setOtherHelp(createValidate.getOtherHelp());
        model.setContractParticulars(createValidate.getContractParticulars());
        model.setPartybEmail(createValidate.getPartybEmail());
        model.setContractAgencyFee(createValidate.getContractAgencyFee());
        model.setIntermediateOfficialFees(createValidate.getIntermediateOfficialFees());
        model.setIntermediaryAgencyFee(createValidate.getIntermediaryAgencyFee());
        model.setTotalIntermediateFunds(createValidate.getTotalIntermediateFunds());
        model.setApplicationPaymentMethod(createValidate.getApplicationPaymentMethod());
        model.setApplicationPaymentMethodid(createValidate.getApplicationPaymentMethodid());
        model.setIsExaminationFee(createValidate.getIsExaminationFee());
        model.setAgencySettlementMethod(createValidate.getAgencySettlementMethod());
        model.setAgencySettlementMethodid(createValidate.getAgencySettlementMethodid());
        model.setNumberDoubleReports(createValidate.getNumberDoubleReports());
        model.setDoubleTotalAgency(createValidate.getDoubleTotalAgency());
        model.setNumberNewPieces(createValidate.getNumberNewPieces());
        model.setNewAgencyFee(createValidate.getNewAgencyFee());
        model.setDeclarationArea(createValidate.getDeclarationArea());
        model.setDeclarationTerm(createValidate.getDeclarationTerm());
        model.setDeclarationRequirement(createValidate.getDeclarationRequirement());
        model.setAccountCredited(createValidate.getAccountCredited());
        model.setAccountCreditedId(createValidate.getAccountCreditedId());
        model.setServiceStandard(createValidate.getServiceStandard());
        model.setFinalOfficialFee(createValidate.getFinalOfficialFee());
        model.setFinalAgencyFee(createValidate.getFinalAgencyFee());
        model.setBalance(createValidate.getBalance());
        model.setLicensingPaymentMethod(createValidate.getLicensingPaymentMethod());
        model.setLicensingPaymentMethodid(createValidate.getLicensingPaymentMethodid());
        model.setFirstthreePaymentMethod(createValidate.getFirstthreePaymentMethod());
        model.setFirstthreePaymentMethodid(createValidate.getFirstthreePaymentMethodid());
        model.setDoubleInventionAgency(createValidate.getDoubleInventionAgency());
        model.setDoubleReportAgency(createValidate.getDoubleReportAgency());
        model.setNumberAppearancePieces(createValidate.getNumberAppearancePieces());
        model.setAppearanceCommission(createValidate.getAppearanceCommission());
        model.setProjectConsultant(createValidate.getProjectConsultant());
        model.setAnnualRegulatoryRequirements(createValidate.getAnnualRegulatoryRequirements());

        // 设置 操作人信息 以及 创建时间
        model.setUserId(Long.valueOf(LikeAdminThreadLocal.getAdminId()));
        model.setUserName(LikeAdminThreadLocal.get("username").toString());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setIsDelete(0);

        listContractMapper.insert(model);
    }

    /**
     * 新建合同扩展信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ListContractUpdateValidate updateValidate) {
        ListContract model = listContractMapper.selectOne(
                new QueryWrapper<ListContract>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setOwnershipDistribution(updateValidate.getOwnershipDistribution());
        model.setOwnershipDistributionId(updateValidate.getOwnershipDistributionId());
        model.setImportanceDegree(updateValidate.getImportanceDegree());
        model.setPartyaEmail(updateValidate.getPartyaEmail());
        model.setTotalContractFees(updateValidate.getTotalContractFees());
        model.setPutdownOfficialfee(updateValidate.getPutdownOfficialfee());
        model.setPutdownAgencyfee(updateValidate.getPutdownAgencyfee());
        model.setPutdownRental(updateValidate.getPutdownRental());
        model.setTermOfPayment(updateValidate.getTermOfPayment());
        model.setAdvance(updateValidate.getAdvance());
        model.setTermOfpaymentExplain(updateValidate.getTermOfpaymentExplain());
        model.setBillingMethod(updateValidate.getBillingMethod());
        model.setInvoiceTitle(updateValidate.getInvoiceTitle());
        model.setNumberOfInventions(updateValidate.getNumberOfInventions());
        model.setInventionCommission(updateValidate.getInventionCommission());
        model.setMiscDigitalComponents(updateValidate.getMiscDigitalComponents());
        model.setOtherAgencyFees(updateValidate.getOtherAgencyFees());
        model.setOtherHelp(updateValidate.getOtherHelp());
        model.setContractParticulars(updateValidate.getContractParticulars());
        model.setPartybEmail(updateValidate.getPartybEmail());
        model.setContractAgencyFee(updateValidate.getContractAgencyFee());
        model.setIntermediateOfficialFees(updateValidate.getIntermediateOfficialFees());
        model.setIntermediaryAgencyFee(updateValidate.getIntermediaryAgencyFee());
        model.setTotalIntermediateFunds(updateValidate.getTotalIntermediateFunds());
        model.setApplicationPaymentMethod(updateValidate.getApplicationPaymentMethod());
        model.setApplicationPaymentMethodid(updateValidate.getApplicationPaymentMethodid());
        model.setIsExaminationFee(updateValidate.getIsExaminationFee());
        model.setAgencySettlementMethod(updateValidate.getAgencySettlementMethod());
        model.setAgencySettlementMethodid(updateValidate.getAgencySettlementMethodid());
        model.setNumberDoubleReports(updateValidate.getNumberDoubleReports());
        model.setDoubleTotalAgency(updateValidate.getDoubleTotalAgency());
        model.setNumberNewPieces(updateValidate.getNumberNewPieces());
        model.setNewAgencyFee(updateValidate.getNewAgencyFee());
        model.setDeclarationArea(updateValidate.getDeclarationArea());
        model.setDeclarationTerm(updateValidate.getDeclarationTerm());
        model.setDeclarationRequirement(updateValidate.getDeclarationRequirement());
        model.setAccountCredited(updateValidate.getAccountCredited());
        model.setAccountCreditedId(updateValidate.getAccountCreditedId());
        model.setServiceStandard(updateValidate.getServiceStandard());
        model.setFinalOfficialFee(updateValidate.getFinalOfficialFee());
        model.setFinalAgencyFee(updateValidate.getFinalAgencyFee());
        model.setBalance(updateValidate.getBalance());
        model.setLicensingPaymentMethod(updateValidate.getLicensingPaymentMethod());
        model.setLicensingPaymentMethodid(updateValidate.getLicensingPaymentMethodid());
        model.setFirstthreePaymentMethod(updateValidate.getFirstthreePaymentMethod());
        model.setFirstthreePaymentMethodid(updateValidate.getFirstthreePaymentMethodid());
        model.setDoubleInventionAgency(updateValidate.getDoubleInventionAgency());
        model.setDoubleReportAgency(updateValidate.getDoubleReportAgency());
        model.setNumberAppearancePieces(updateValidate.getNumberAppearancePieces());
        model.setAppearanceCommission(updateValidate.getAppearanceCommission());
        model.setProjectConsultant(updateValidate.getProjectConsultant());
        model.setAnnualRegulatoryRequirements(updateValidate.getAnnualRegulatoryRequirements());

        // 设置 操作人信息 以及 更新时间
        model.setUpdateUserId(Long.valueOf(LikeAdminThreadLocal.getAdminId()));
        model.setUpdateUserName(LikeAdminThreadLocal.get("username").toString());
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));

        listContractMapper.updateById(model);
    }

    /**
     * 新建合同扩展信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ListContract model = listContractMapper.selectOne(
                new QueryWrapper<ListContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        listContractMapper.updateById(model);
    }

}
